<?php
/**
 * Created by LíJimpéy(：
 * User: jimpey88
 * Date: 2024/04/08
 * Email: < jimpey@qq.com >
 * Project: company-admin-tp6
 */
declare (strict_types = 1);

namespace app\backend_api\controller;

use app\backend_api\model\UploadFiles;
use app\BaseController;
use think\facade\Filesystem;

class Upload extends BaseController
{
    /**
     * 文件上传
     * @return \think\response\Json
     */
    public function file(): \think\response\Json
    {
        $file = request()->file('file');
        if (!$file) {
            return $this->error('请上传文件！');
        }
//        dd(Filesystem::getDiskConfig('public', 'url'));
        $saveName = Filesystem::disk('public')->putFile( '', $file);
        $file_name = str_replace('\\', '/', $saveName);
        $viewPath = Filesystem::getDiskConfig('public', 'url') . '/' . $file_name;
        $host_url = $this->request->domain();
        $url = $host_url . $viewPath;
        $storage = 'local';

        UploadFiles::create([
            'user_id' => request()->admin_id,
            'storage' => $storage,
            'host_url' => $host_url,
            'file_name' => $file_name,
            'file_size' => $file->getSize(),
            'file_type' => $file->getMime(),
            'extension' => $file->getOriginalExtension(),
        ]);

        return $this->success($url, '上传成功');
    }
}
